package com.frame.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.frame.Page;
import com.frame.Result;
import com.frame.domain.Department;
import com.frame.service.DeptService;
import com.frame.support.Tree;
import com.mini.core.utils.StrKit;

/**
 * @author sxjun1904
 * @since 2016-8-8
 */
@Controller
@RequestMapping("/dept")
public class DeptController extends BaseController{

    private static final Logger log = LoggerFactory.
            getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    /**
     * 部门页面
     * @param model
     * @param request
     * @param response
     * @param treeId
     * @return
     */
    @RequiresPermissions("dept:list")
    @RequestMapping(value = "list")
    public String list(Model model, HttpServletRequest request, HttpServletResponse response,String treeId) {
    	Page<Department> page = deptService.paginateResult(new Page<Department>(request, response),treeId);
    	addPage(model,page);
        return "dept/deptList";
    }
    
    /**
     * 部门表单页面
     * @param model
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping(value = "form")
    public String form(Model model, HttpServletRequest request, HttpServletResponse response,String id) {
    	if(StrKit.notBlank(id)){
    		Department m = deptService.findById(id);
    		model.addAttribute("dept", m);
    	}
        return "dept/deptForm";
    }
    
    /**
     * 保存部门
     * @param dept
     * @return
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public Result save(Department dept) {
    	if(Tree.TREE_ROOT_ID.equals(dept.getParentid()))
    		dept.setParentid(null);
    	if(StrKit.notBlank(dept.getId()))
    		deptService.update(dept);
    	else{
    		dept.setCreatetime(new Date());
        	deptService.save(dept);
    	}
        return success();
    }
    
    /**
     * 删除部门
     * @param id
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public Result delete(String id) {
    	deptService.delete(id);
        return success();
    }
    
    /**
     * 获取部门树
     * @return
     */
    @RequestMapping(value = "tree")
    @ResponseBody
    public List<Tree> tree() {
    	List<Department> depts = deptService.findAll();
    	List<Tree> trees = new ArrayList<Tree>();
    	Tree root = new Tree();
    	root.setId(Tree.TREE_ROOT_ID);
    	root.setName("部门树");
    	root.setOpen(true);
    	trees.add(root);
    	for(Department dept : depts){
    		Tree tree = new Tree();
    		tree.setId(dept.getId());
    		tree.setName(dept.getName());
    		if(StrKit.isBlank(dept.getParentid())){
    			tree.setpId(Tree.TREE_ROOT_ID);
	    		tree.setOpen(true);
    		}else
    			tree.setpId(dept.getParentid());
    		trees.add(tree);
    	}
    	return trees;
    }
    
}
